package com.zero.xiaodai.utils;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Strings;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSFile;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class DocUtil {

	@Autowired
	private static Configuration freeMarker;

	public static void createDoc(Map<String, Object> map, HttpServletResponse response, HttpServletRequest request)
			throws Exception {

		freeMarker = freeMarker == null ? new Configuration() : freeMarker;
		freeMarker.setClassForTemplateLoading(DocUtil.class, "/template");
		freeMarker.setDefaultEncoding("utf-8");
		// 获取模板实例
		Template template = freeMarker.getTemplate("agreement.ftl");
		// 通过Map传递动态数据
		String wordName=String.valueOf(map.get("goodsAssortmentName"))+String.valueOf(map.get("type"))+"合同"+String.valueOf(map.get("agreementNo"));
		
		File outFile = new File(wordName+".doc"); // 导出文件
		// response.getOutputStream()
		Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
		template.process(map, out);
		/*OutputStream outputStream = response.getOutputStream();
		outputStream.write(out.toString().getBytes());
		outputStream.flush();
		outputStream.close();*/

		
		// 写明要下载的文件的大小
		response.setContentLength((int) outFile.length());
		response.setHeader("Content-Disposition", "attachment;filename=" + outFile.getName());// 设置在下载框默认显示的文件名
		response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
		// 读出文件到response
		// 这里是先需要把要把文件内容先读到缓冲区
		// 再把缓冲区的内容写到response的输出流供用户下载
		FileInputStream fileInputStream = new FileInputStream(outFile);
		
		
		BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
		byte[] b = new byte[bufferedInputStream.available()];
		bufferedInputStream.read(b);
		OutputStream outputStream = response.getOutputStream();
		outputStream.write(b);
		// 人走带门
		bufferedInputStream.close();
		outputStream.flush();
		outputStream.close();
		
	}
	
	
	
	public static String createDocPath(Map<String, Object> map, HttpServletResponse response, HttpServletRequest request)
			throws Exception {

		freeMarker = freeMarker == null ? new Configuration() : freeMarker;
		freeMarker.setClassForTemplateLoading(DocUtil.class, "/template");
		freeMarker.setDefaultEncoding("utf-8");
		// 获取模板实例
		Template template = freeMarker.getTemplate("agreement.ftl");
		// 通过Map传递动态数据

		String wordName=String.valueOf(map.get("goodsAssortmentName"))+String.valueOf(map.get("type"))+"合同"+String.valueOf(map.get("agreementNo"));
		
		File outFile = new File(wordName+".doc"); // 导出文件// response.getOutputStream()
		Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
		template.process(map, out);
		/*OutputStream outputStream = response.getOutputStream();
		outputStream.write(out.toString().getBytes());
		outputStream.flush();
		outputStream.close();*/

		
		// 写明要下载的文件的大小
//		response.setContentLength((int) outFile.length());
//		response.setHeader("Content-Disposition", "attachment;filename=" + outFile.getName());// 设置在下载框默认显示的文件名
//		response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
		// 读出文件到response
		// 这里是先需要把要把文件内容先读到缓冲区
		// 再把缓冲区的内容写到response的输出流供用户下载
		FileInputStream fileInputStream = new FileInputStream(outFile);
		
		GridFS gridFS = new GridFS(MongoDB.getDB(MongoProperties.DB_NAME), MongoProperties.COLLECTIONS_NAME);
		GridFSFile fsFile = gridFS.createFile(fileInputStream, outFile.getName());
		fsFile.save();
		return "/" + MongoProperties.NGINX_PATH + "/" + outFile.getName();
		
	}

}
